{% extends "myadmin/base.html" %}

{% block mainbody %}                
    <h3>
        商品信息管理
    </h3>

    <!--编辑商品信息form开始-->
    <form id="edit-profile" name="goodsform" action="{% url 'myadmin_goods_update' goods.id %}" class="form-horizontal" method="post" onsubmit="return check(this)" enctype="multipart/form-data">
        {% csrf_token %}
        <fieldset>
            <legend>查看/编辑商品信息</legend>
            <div class="control-group">
                <label class="control-label" for="input01">商品类别：</label>
                <div class="controls">
                    <select name="typeid" id="input01">
                        {% for vo in typelist %}
                            <option 
                                {% if vo.pid == 0 %}
                                    disabled
                                {% endif %} 
                                {% if vo.id == goods.typeid %}
                                    selected
                                {% endif %} 
                                 value="{{ vo.id }}">{{vo.pname}}|--{{ vo.name }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input02">商品名称：</label>
                <div class="controls">
                    <input type="text" name="goods" class="input-xlarge" id="input02" value="{{goods.goods}}"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input03">生产厂家：</label>
                <div class="controls">
                    <input type="text" name="company" class="input-xlarge" id="input03" value="{{goods.company}}"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input04">单价：</label>
                <div class="controls">
                    <input type="text" name="price" class="input-xlarge" id="input04" value="{{goods.price}}"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input05">库存量：</label>
                <div class="controls">
                    <input type="text" name="store" class="input-xlarge" id="input05" value="{{goods.store}}"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input06">销售量：</label>
                <div class="controls">
                    <input type="text" name="num" class="input-xlarge" id="input06" value="{{goods.num}}" readonly="true"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input07">点击次数：</label>
                <div class="controls">
                    <input type="text" name="clicknum" class="input-xlarge" id="input07" value="{{goods.clicknum}}" readonly="true"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input08">添加时间：</label>
                <div class="controls">
                    <input type="text" name="addtime" class="input-xlarge" id="input08" value="{{goods.addtime}}" readonly="true"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input09">商品图片：</label>
                <div class="controls">
                    <input type="file" name="pic" class="input-xlarge" id="input09"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input10">状态：</label>
                <div class="controls">
                    <input type="radio" name="state" class="input-xlarge" id="input10" value="1" 
                    {% if goods.state == 1 %}checked{% endif %} /> 新添加 
                    <input type="radio" name="state" class="input-xlarge" id="input10" value="2" 
                    {% if goods.state == 2 %}checked{% endif %} /> 在售
                    <input type="radio" name="state" class="input-xlarge" id="input10" value="3" 
                    {% if goods.state == 3 %}checked{% endif %} /> 已下架
                </div>
            </div>  
            <!--<div class="control-group">
                <label class="control-label" for="input11">商品简介：</label>
                <div class="controls">
                    <textarea cols="40" style="width:450px" rows="10" name="content" id="input11">{{goods.content}}</textarea>
                </div>
            </div>-->
            <div class="control-group">
                <label for="editor" class="control-label">商品简介：</label>
                <div class="controls">
                    <textarea id="editorex" name="contectex" class="" rows="10" style="display:none">{{goods.content}}</textarea>
                    <!-- <script id="editor" type="text/plain" style="width:100%;height:500px;"></script> -->
                    <script id="editor" name="content" type="text/plain" style="width:600px;height:450px;"></script>
                </div>
            </div>                          
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">保存</button> <button type="reset" class="btn">重置</button>
            </div>
            <div class="control-group">
                <label class="control-label" for="img01"></label>
                <div class="controls">
                    <img src="/static/goods/{{ goods.picname }}" width="375" id="img01"/>
                </div>
            </div>
        </fieldset>
    </form>
    <!--编辑商品信息form结束-->

    <!--富文本编辑器脚本开始-->
    <script type="text/javascript">
        var ue = UE.getEditor('editor');
        SyntaxHighlighter.all();
        ue.addListener("ready", function() {
            ue.setContent($("#editorex").val());
        });
        
        //$("#editor").innerHTML = Server.HtmlDecode($("#editor").innerHTML);
    </script>
    <!--富文本编辑器脚本结束-->

    <script type="text/javascript">
        //执行点击“选择文件”按钮，显示选择的图片文件
        $("input[type='file']").change(function(){   
            var file = this.files[0];
            if (window.FileReader) {    
                var reader = new FileReader();    
                reader.readAsDataURL(file);    
                //监听文件读取结束后事件    
                reader.onloadend = function (e) {
                    $("#img01").attr("src",e.target.result);    //img.src设置文件最后的路径地址
                };    
            } 
        }); 

        //表单提交前进行数据校验
        function check(form){
            //校验商品名称是否非空
            if (form.goods.value == ""){
                alert("请输入商品名称！");
                form.goods.focus();
                return false;
            }

            //校验单价是否非空且为数字
            var patrn = /^[0-9]*.[0-9]*$/;
            var val = form.price.value;
            if (val == ""){
                alert("请输入单价！");
                form.price.focus();
                return false;
            }
            else{
                if (!patrn.test(val)) {  
                    alert("单价包含不是数字的字符！");
                    form.price.focus();
                    return false;
                }  
            }

            //校验库存量是否非空且为数字
            patrn = /^[0-9]*$/;
            val = form.store.value;
            if (val == ""){
                alert("请输入库存量！");
                form.store.focus();
                return false;
            }
            else{
                if (!patrn.test(val)) {  
                    alert("库存量包含不是数字的字符！");
                    form.store.focus();
                    return false;
                  }  
            }
            
            return true;
        }
    </script>
{% endblock %}